﻿using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Enums;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;
using YouWei.Op.Common;
using YouWei.Op.Models.User;

namespace YouWei.Op.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    public class UserController : BaseController
    {
        UserInfoService usrService = new UserInfoService();
        // GET: User
        public ActionResult Index()
        {
            ViewBag.GroupID = GetParam("groupid");
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ContentResult List(UserSearchModel model)
        {
            int pageIndex = GetPageIndex();
            int pageSize = GetPageSize();
            Page<UserInfoPageModel> page = usrService.Pages(pageIndex, pageSize, model);
            foreach (var item in page.rows)
            {
                item.PayStateName = Utility.EnumHelper.GetEnumDesc(typeof(PayStateEnum), item.PayState);
            }
            return ReturnResult(page);
        }

        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Detail(int userId)
        {
            UserInfo usr = usrService.Single(userId);
            return View(usr);
        }

        /// <summary>
        /// 获取当前用户的子用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ContentResult ChildList()
        {
            int pageIndex = GetPageIndex();
            int pageSize = GetPageSize();
            int userId = GetParam<int>("userId");
            Page<UserInfo> page = usrService.Pages(pageIndex, pageSize, userId, null);
            return ReturnResult(page);
        }
        [HttpPost]
        public ContentResult SetVehileStatus(UserInfo model)
        {
            ResultData<int> rt = ResultData<int>.Error("");

            if (model.UserID <= 0)
            {
                rt.message = "缺少必须的参数UserID";
                return ReturnResult(rt);
            }

            if (model.VehicleStatus <= 0)
            {
                rt.message = "缺少必要的参数VehicleStatus";
                return ReturnResult(rt);
            }
            var entity = usrService.Single(model.UserID);
            if (entity != null)
            {
                entity.VehicleStatus = model.VehicleStatus;
                entity.UpdateDate = DateTime.Now;
                var re = usrService.Update(entity);
                if (re)
                {
                    rt.message = "更新成功";
                    rt.status = 0;
                }
                else
                {
                    rt.message = "更新失败";
                }
            }
            else
            {
                rt.message = "未找到用户，检查UserID是否正确";
            }
            return ReturnResult(rt);
        }

        [HttpGet]
        public ContentResult GetAllUserList()
        {
            List<UserInfo> page = usrService.QueryAll();
            return ReturnResult(page);
        }
    }

  
}